<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/26
 * Time: 16:49
 */
namespace app\admin\service;

use app\admin\model\FreightModel;

class ExpressService
{
    function shippingFeeDetail(){
        $freight=new FreightModel();
        $freight_list=$freight->select();
        if($freight_list){
            $address=new AddressService();
            $province = $address->getProvinceList();
            $city = $address->getCityList();
            $province_array = explode(",", $freight_list["province_id_array"]);
            $city_array = explode(",", $freight_list["city_id_array"]);
        }else{
            return null;
        }
    }
    /**
     * 修改运费模板
     */
    function updateShippingFee($tpl_id,$shipping_fee_id,  $shipping_fee_name, $province_id_array, $city_id_array, $district_id_array, $weight_snum, $weight_sprice, $weight_xnum, $weight_xprice,  $volume_snum, $volume_sprice, $volume_xnum, $volume_xprice,  $bynum_snum, $bynum_sprice, $bynum_xnum, $bynum_xprice,$data_index){
        $freight=new FreightModel();
        $freight->startTrans();
        try {
            $data = array(
                'tpl_id' => $tpl_id,
                'province_id_array' => $province_id_array,
                'city_id_array' => $city_id_array,
                'district_id_array' => $district_id_array,
                'weight_snum' => $weight_snum,
                'weight_xnum' => $weight_xnum,
                'weight_sprice' => $weight_sprice,
                'weight_xprice' => $weight_xprice,
                'volume_snum' => $volume_snum,
                'volume_sprice' => $volume_sprice,
                'volume_xnum' => $volume_xnum,
                'volume_xprice' => $volume_xprice,
                'bynum_snum' => $bynum_snum,
                'bynum_sprice' => $bynum_sprice,
                'bynum_xnum' => $bynum_xnum,
                'bynum_xprice' => $bynum_xprice,
                'update_time' => time(),
                'is_whole' => $data_index,
            );
            $freight->save($data, [
                'shipping_fee_id' => $shipping_fee_id
            ]);

            $freight->commit();
            return 1;
        } catch (\Exception $e) {
            $freight->rollback();
            return $e->getMessage();
        }
        return - 1;
    }
    /**
     * 添加运费模板
     */
    function addShippingFee($tpl_id,$shipping_fee_name, $province_id_array, $city_id_array, $district_id_array,  $weight_snum, $weight_sprice, $weight_xnum, $weight_xprice,  $volume_snum, $volume_sprice, $volume_xnum, $volume_xprice,  $bynum_snum, $bynum_sprice, $bynum_xnum, $bynum_xprice,$data_index){
        $freight=new FreightModel();
        $freight->startTrans();
        try {
            $data = array(
                'tpl_id' => $tpl_id,
                'shipping_fee_name' => $shipping_fee_name,
                'province_id_array' => $province_id_array,
                'city_id_array' => $city_id_array,
                'district_id_array' => $district_id_array,
                'weight_snum' => $weight_snum,
                'weight_xnum' => $weight_xnum,
                'weight_sprice' => $weight_sprice,
                'weight_xprice' => $weight_xprice,
                'volume_snum' => $volume_snum,
                'volume_sprice' => $volume_sprice,
                'volume_xnum' => $volume_xnum,
                'volume_xprice' => $volume_xprice,
                'bynum_snum' => $bynum_snum,
                'bynum_sprice' => $bynum_sprice,
                'bynum_xnum' => $bynum_xnum,
                'bynum_xprice' => $bynum_xprice,
                'create_time' => time(),
                'is_whole' => $data_index,
            );
            $freight->save($data);
            $freight->commit();
            return 1;
        } catch (\Exception $e) {
            $freight->rollback();
            return $e->getMessage();
        }
        return - 1;
    }
}